package com.jnsw.testkotlin;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ImageSActivity extends AppCompatActivity {
    GridView gv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_s);

        List<Map<String, Object>> list =  new ArrayList<Map<String, Object>>();
        for (int i = 0 ; i < 9 ;i ++){
            Map<String, Object> map =new HashMap<String,Object>();
            map.put("title","title"+i);
            if (i%2 == 1){

                map.put("ic","http://www.jcodecraeer.com/uploads/20140731/67391406772378.png");
            }else {
                map.put("ic","http://www.jcodecraeer.com/uploads/201140731/67391406772378.png");

            }
            list.add(map);
        }

        String[] strs= {"title","ic"};
        int[] ids = {R.id.text1,R.id.icon};
        SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), list , R.layout.item, strs,ids);
        adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, Object data, String textRepresentation) {
                //判断是否为我们要处理的对象
                if (view instanceof ImageView && data instanceof String) {
                    ImageView iv = (ImageView) view;
                    if (!"".equals((String) data)) {
                        Picasso.with(ImageSActivity.this)
                                .load(data.toString())
                                .placeholder(R.drawable.ic_menu_share)
                                .error(R.drawable.ak)
                                .into(iv);

                    } else {
                        iv.setImageDrawable(getResources().getDrawable(R.drawable.ak));
                    }
                    return true;
                } else if (view instanceof TextView && data instanceof String)  {
                    TextView tv =      ((TextView) view);
                tv  .setText(data.toString());
                    return true;
                }else {
                    return false;

                }
            }
        });

gv = (GridView) findViewById(R.id.gv);
        gv.setAdapter(adapter);


    }
}
